package com.kang.controller;

import com.kang.commons.Result;
import com.kang.commons.util.EmailUtil;
import com.kang.bean.Email;
import com.kang.bean.Users;
import com.kang.dto.UsersDto;
import com.kang.service.UserService;
import com.kang.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: photo_fawu
 * @description:
 * @author: K.faWu
 * @create: 2021-11-04 16:56
 **/

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UsersService usersService;


    @Autowired
    private EmailUtil emailUtil;



    /**
     * 发送邮件
     */
    @GetMapping("/test")
    public Result<?> sendEmail() throws Exception {
        Email email = new Email();
        email.setReceiver("1551360792@qq.com");
        email.setSubject("你好，世界！");
        email.setContent("hello world!");
        emailUtil.sendText(email);
        return Result.success();
    }

    /*
     * 判断用户权限
     */
/*    @GetMapping("/role")
    public Result isRole(HttpServletRequest request){
        userService.isRole(request);
        return Result.success();
    }*/

    /*
     * 后端使用
     * 获取用户列表
     */
//    @PostMapping("/getAll")
//    public Result getAll(@RequestBody Users users, Pageable page, HttpServletRequest request){
//        PageInfo<List<Users>> pageInfo= userService.getAll(users, page,request);
//        if (pageInfo.getList() == null || pageInfo.getList().size() == 0)
//            return Result.failure("发现越权行为！对用户及ip进行进行警告！");
//        return Result.success(pageInfo);
//    }

    /**
     * 修改用户信息
     */
    @PostMapping("/update")
    public Result<?> updateUser(@RequestBody UsersDto usersDto){
        return usersService.updateUser(usersDto);

    }

    /*
     * 根据手机号搜索用户
     */
//    @GetMapping("/phone")
//    public Result getByPhone(String phone){
//        List<Map<String, String>> list = userService.getByPhone(phone);
//        return Result.success(list);
//    }

    /**
     * 获取登录的用户
     */
    @GetMapping
    public Result<?> getUsers(HttpServletRequest request){
        Users users = userService.getUsers(request);
        return Result.success(users);
    }

    /**
     * 获取指定id的用户信息
     */
    @GetMapping("/{id}")
    public Result<?> getUsersById(@PathVariable Long id){
        return usersService.getUsersById(id);
    }

    /*
     * 退出登录
     */
//    @GetMapping("/out")
//    public Result outUser(HttpServletRequest request){
//        userService.outUser(request);
//        return Result.success();
//    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody UsersDto usersDto){
        //手机密码验证
        usersDto.setType(1);
        return usersService.login(usersDto);
    }

    /*
     * 注册用户
     */
//    @PostMapping("/register")
//    public Result register(@RequestBody UsersVo usersVo){
//        userService.register(usersVo);
//        return Result.success();
//    }

    /**
     * 上传头像
     */
    @PostMapping("/upload")
    public Result<?> upload(@RequestParam MultipartFile file){
        String path = userService.upload(file);
        return Result.success(path);
    }

    /*
     * 发送验证码
     */
//    @GetMapping("/register/{phone}")
//    public Result getVerificationCode(@PathVariable String phone){
//        String resutl = userService.getVerificationCode(phone);
//        if (StringUtils.isEmpty(resutl)){
//            return Result.success();
//        }
//        return Result.failure(resutl);
//    }
}
